﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TCCUIControls
{
    public partial class UIProcessor : UserControl
    {
        private int m_Padding = 5;
        private Brush m_Brush = new SolidBrush(Color.Khaki);
        private Brush m_WhiteBrush = new SolidBrush(Color.White);
        private Brush m_TextBrush = new SolidBrush(Color.Black);
        private Pen m_Pen = new Pen(Color.Gray);
        private static Font m_RegularFont = null;
        private string m_Name;
        private string m_Priority;

        public UIProcessor(string name, CommandListView cacheListView)
        {
            m_Name = name;
            this.cacheListView = cacheListView;

            InitializeComponent();

            if (m_RegularFont == null)
                m_RegularFont = new Font(pnlName.Font, FontStyle.Regular);
        }

        private void pnlName_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            Rectangle rect = pnlName.ClientRectangle;
            rect.Width--;
            rect.Height--;

            g.FillRectangle(m_WhiteBrush, rect);
            g.DrawRectangle(m_Pen, rect);

            rect.X += m_Padding;
            rect.Y += m_Padding;
            rect.Width -= 2 * m_Padding;
            rect.Height -= 2 * m_Padding;

            g.FillRectangle(m_Brush, rect);
            g.DrawRectangle(m_Pen, rect);

            rect.X += m_Padding;
            rect.Y += m_Padding;
            rect.Width -= 2 * m_Padding;
            rect.Height -= 2 * m_Padding;

            SizeF textSize = g.MeasureString(m_Name, pnlName.Font);
            SizeF textSize2 = g.MeasureString(m_Priority, m_RegularFont);
            g.DrawString(m_Name, pnlName.Font, m_TextBrush, rect.X + rect.Width / 2 - (textSize.Width + textSize2.Width) / 2, rect.Y);
            g.DrawString(m_Priority, pnlName.Font, m_TextBrush, rect.X + textSize.Width + rect.Width / 2 - (textSize.Width + textSize2.Width) / 2, rect.Y);
        }

        public void EnqueueTask(string str)
        {
            lstCommands.Items.Add(str);
        }
        delegate void ProceedToCommandDelegate(int index, string str);
        public void ProceedToCommand(int index, string str)
        {
            if (index < 0 || index >= lstCommands.Items.Count)
                return;

            if (lstCommands.InvokeRequired)
            {
                lstCommands.BeginInvoke(new ProceedToCommandDelegate(ProceedToCommand), index, str);
                return;
            }

            lstCommands.CurrentIndex = index;
            lstCommands.Items[index].Text = str;
            lstCommands.Invalidate();
        }
        public void SetPriority(string str)
        {
            m_Priority = str;
            pnlName.Invalidate();
        }
    }
}
